﻿package mortal.game.view.copy.guildDefenseCopy
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import extend.language.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.utils.*;
    import mortal.game.view.activeTime.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.guildDefenseCopy.panel.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuildDefenseController extends CopyControllerBase
    {
        private var _module:GuildDefenseCopyModule;
        private var _miniMapView:GuildDefenseMiniMap;
        private var _hasEndActive:Boolean;
        private var _lastLevel:int;

        public function GuildDefenseController()
        {
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._module == null)
            {
                this._module = new GuildDefenseCopyModule();
                this._module.addEventListener(WindowEvent.SHOW, this.onShow);
                this._module.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._module;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.GuildDefenceCopyOpen, this.copyOpen);
            NetDispatcher.addCmdListener(ServerCommand.GuildDefenceInfoUpdate, this.updateCopyInfo);
            NetDispatcher.addCmdListener(ServerCommand.GUILD_INFO_UPDATE, this.copyOpen);
            NetDispatcher.addCmdListener(ServerCommand.GuildDefenceCopyEnd, this.iconHide);
            NetDispatcher.addCmdListener(ServerCommand.GUILD_INFO_UPDATE, this.checkActiveIcon);
            NetDispatcher.addCmdListener(ServerCommand.NewDayUpdate, this.newDayUpdate);
            Dispatcher.addEventListener(EventName.GuildDefenseCopyEnter, this.enterGuildDefenceCopyHandler);
            Dispatcher.addEventListener(EventName.GuildDefenseWinOpen, this.openWin);
            Dispatcher.addEventListener(EventName.GuildDefenseTowerUpgrade, this.towerUpgrade);
            Dispatcher.addEventListener(EventName.ActiveStart, this.activesStartHandler);
            Dispatcher.addEventListener(EventName.ActiveNotic, this.activesNoticHandler);
            Dispatcher.addEventListener(EventName.ActiveEnd, this.activesEndHandler);
            return;
        }// end function

        private function onShow(event:WindowEvent) : void
        {
            return;
        }// end function

        private function onClose(event:WindowEvent) : void
        {
            return;
        }// end function

        private function newDayUpdate(param1:Object) : void
        {
            this._hasEndActive = false;
            return;
        }// end function

        override public function enterCopy(param1:TCopy) : void
        {
            super.enterCopy(param1);
            this.addMiniMap();
            if (GameController.guild.isViewShow)
            {
                GameController.guild.view.hide();
            }
            if (this.isViewShow)
            {
                this.view.hide();
            }
            return;
        }// end function

        override public function leaveCopy() : void
        {
            super.leaveCopy();
            if (this._miniMapView)
            {
                this._miniMapView.hide();
                this._miniMapView = null;
            }
            return;
        }// end function

        private function addMiniMap() : void
        {
            if (this._miniMapView == null)
            {
                this._miniMapView = new GuildDefenseMiniMap();
            }
            this._miniMapView.show();
            return;
        }// end function

        private function openGuildDefenceCopy(event:DataEvent) : void
        {
            return;
        }// end function

        private function enterGuildDefenceCopyHandler(event:DataEvent) : void
        {
            var _loc_2:* = Cache.instance.guild.selfGuildInfo.baseInfo.level;
            CopyUtil.copyCheck(this.enterGuildDefenceCopy, _loc_2);
            return;
        }// end function

        private function enterGuildDefenceCopy(param1:int) : void
        {
            GameProxy.copy.enterGuildDefenseCopy(param1);
            return;
        }// end function

        private function openWin(event:DataEvent) : void
        {
            var _loc_2:int = 2;
            if (Cache.instance.guild.selfGuildInfo.baseInfo.level < _loc_2)
            {
                return MsgManager.showRollTipsMsg(Language.getStringByParam(35252, _loc_2));
            }
            if (!ActiveTimeUtil.isInDoubleTimeByType(EActiveType._EActiveTypeGuildDefense))
            {
                return MsgManager.showRollTipsMsg(Language.getString(35266), MsgRollTipsType.msgRollTips3);
            }
            view.show();
            return;
        }// end function

        private function copyOpen(param1:Object = null) : void
        {
            var _loc_2:SClientActive = null;
            if (Cache.instance.copy.guildDefence.isOpenCopy && Cache.instance.guild.selfGuildInfo && Cache.instance.guild.selfGuildInfo.baseInfo && Cache.instance.guild.selfGuildInfo.baseInfo.level >= 2)
            {
                if (this._lastLevel == Cache.instance.guild.selfGuildInfo.baseInfo.level)
                {
                    return;
                }
                this._lastLevel = Cache.instance.guild.selfGuildInfo.baseInfo.level;
                GuildDefenseIcon.instance.show();
                GuildDefenseIcon.instance.playEffect();
                view.show();
                _loc_2 = ActiveTimeUtil.getSClientActive(EActiveType._EActiveTypeGuildDefense);
                if (_loc_2)
                {
                    GuildDefenseIcon.instance.setLeftTimeAndState((_loc_2.endDt.time - ClockManager.instance.nowDate.time) * 0.001, 2);
                }
                else
                {
                    GuildDefenseIcon.instance.setLeftTimeAndState(0, 2);
                }
            }
            return;
        }// end function

        private function updateCopyInfo(param1:SGuildDefenseInfo) : void
        {
            if (this._miniMapView)
            {
                this._miniMapView.updateAll(param1);
            }
            return;
        }// end function

        private function towerUpgrade(event:DataEvent) : void
        {
            GameProxy.copy.upgradeTower(event.data.type, event.data.num);
            return;
        }// end function

        private function iconHide(param1:Object = null) : void
        {
            if (this.isViewShow)
            {
                view.hide();
            }
            if (GuildDefenseIcon.isviewShow)
            {
                GuildDefenseIcon.instance.hide();
            }
            this._hasEndActive = true;
            return;
        }// end function

        private function checkActiveIcon(param1:Object) : void
        {
            var _loc_3:SClientActive = null;
            var _loc_2:* = ActiveTimeUtil.isInDoubleTimeByType(EActiveType._EActiveTypeGuildDefense);
            if (_loc_2 && Cache.instance.guild.selfGuildInfo && Cache.instance.guild.selfGuildInfo.baseInfo && Cache.instance.guild.selfGuildInfo.baseInfo.level >= 2)
            {
                _loc_3 = ActiveTimeUtil.getSClientActive(EActiveType._EActiveTypeGuildDefense);
                GuildDefenseIcon.instance.show();
                GuildDefenseIcon.instance.setLeftTimeAndState((_loc_3.endDt.time - ClockManager.instance.nowDate.time) * 0.001, 2);
            }
            return;
        }// end function

        private function activesStartHandler(event:DataEvent) : void
        {
            if (this._hasEndActive)
            {
                return;
            }
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type == EActiveType._EActiveTypeGuildDefense)
            {
                if (Cache.instance.guild.selfGuildInfo && Cache.instance.guild.selfGuildInfo.baseInfo && Cache.instance.guild.selfGuildInfo.baseInfo.level >= 2)
                {
                    GuildDefenseIcon.instance.show();
                    GuildDefenseIcon.instance.setLeftTimeAndState((_loc_2.endDt.time - ClockManager.instance.nowDate.time) * 0.001, 2);
                }
            }
            return;
        }// end function

        private function activesNoticHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type == EActiveType._EActiveTypeGuildDefense)
            {
                if (Cache.instance.guild.selfGuildInfo && Cache.instance.guild.selfGuildInfo.baseInfo && Cache.instance.guild.selfGuildInfo.baseInfo.level >= 2)
                {
                    GuildDefenseIcon.instance.show();
                    GuildDefenseIcon.instance.setLeftTimeAndState((_loc_2.startDt.time - ClockManager.instance.nowDate.time) * 0.001, 1);
                }
            }
            return;
        }// end function

        private function activesEndHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type == EActiveType._EActiveTypeGuildDefense)
            {
                if (GuildDefenseIcon.isviewShow)
                {
                    GuildDefenseIcon.instance.hide();
                }
            }
            return;
        }// end function

    }
}
